package org.bear.mario.Pojo;

import org.bear.mario.Running.BackGround;
import org.bear.mario.Running.StaticValue;

import java.awt.image.BufferedImage;

// 敌人类，代表游戏中的敌人，具有移动和死亡的行为
public class Enemy implements Runnable {
    private int x; // 敌人的当前x坐标
    private int y; // 敌人的当前y坐标
    private int startx; // 敌人的起始x坐标
    private int starty; // 敌人的起始y坐标
    private int type; // 敌人的类型
    private BufferedImage showImage; // 敌人的显示图像
    private boolean isLeftOrUp = true; // 敌人当前移动方向标识
    private int upMax = 0; // 垂直移动的最大上限
    private int downMax = 0; // 垂直移动的最大下限
    private Thread t = null; // 用于处理敌人移动的线程
    private int imageType = 0; // 当前显示的图像类型
    private BackGround bg; // 背景对象，包含所有障碍物和敌人

    // 构造函数，初始化敌人位置、方向、类型和背景对象
    public Enemy(int x, int y, boolean isLeft, int type, BackGround bg) {
        this.x = x;
        this.y = y;
        this.startx = x;
        this.starty = y;
        this.isLeftOrUp = isLeft;
        this.type = type;
        this.bg = bg;
        if (type == 1) {
            this.showImage = StaticValue.allTriangleImage.get(0);
        }
        this.t = new Thread(this);
        t.start();
        t.suspend();
    }

    // 构造函数，初始化敌人位置、方向、类型、移动范围和背景对象
    public Enemy(int x, int y, boolean isUp, int type, int upMax, int downMax, BackGround bg) {
        this.x = x;
        this.y = y;
        this.startx = x;
        this.starty = y;
        this.isLeftOrUp = isUp;
        this.type = type;
        this.upMax = upMax;
        this.downMax = downMax;
        this.bg = bg;
        if (type == 2) {
            this.showImage = StaticValue.allFlowerImage.get(0);
        }
        this.t = new Thread(this);
        t.start();
        t.suspend();
    }

    // 线程运行的方法，控制敌人的移动和图像更新
    public void run() {
        while (true) {
            if (type == 1) {
                if (this.isLeftOrUp) {
                    this.x -= 5;
                } else {
                    this.x += 5;
                }
                if (imageType == 0) {
                    imageType = 1;
                } else {
                    imageType = 0;
                }

                boolean canLeft = true;
                boolean canRight = true;

                for (int i = 0; i < this.bg.getAllObstruction().size(); i++) {
                    Obstruction ob = this.bg.getAllObstruction().get(i);
                    if (ob.getX() == this.x + 60 && (ob.getY() + 50 > this.y && ob.getY() - 50 < this.y)) {
                        canRight = false;
                    }
                    if (ob.getX() == this.x - 60 && (ob.getY() + 50 > this.y && ob.getY() - 50 < this.y)) {
                        canLeft = false;
                    }
                }

                if (!canLeft && this.isLeftOrUp || this.x == 0) {
                    this.isLeftOrUp = false;
                } else if (!canRight && !this.isLeftOrUp || this.x == 840) {
                    this.isLeftOrUp = true;
                }

                this.showImage = StaticValue.allTriangleImage.get(imageType);
            }
            if (type == 2) {
                if (this.isLeftOrUp) {
                    this.y -= 5;
                } else {
                    this.y += 5;
                }
                if (imageType == 0) {
                    imageType = 1;
                } else {
                    imageType = 0;
                }
                if (this.isLeftOrUp && this.y == this.upMax) {
                    this.isLeftOrUp = false;
                }
                if (!this.isLeftOrUp && this.y == this.downMax) {
                    this.isLeftOrUp = true;
                }
                this.showImage = StaticValue.allFlowerImage.get(imageType);
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    // 重置敌人的位置为起始位置
    public void reset() {
        this.x = this.startx;
        this.y = this.starty;
        if (this.type == 1) {
            this.showImage = StaticValue.allTriangleImage.get(0);
        } else if (this.type == 2) {
            this.showImage = StaticValue.allFlowerImage.get(0);
        }
    }

    // 敌人死亡处理，更新图像并将其从背景中移除
    public void dead() {
        this.showImage = StaticValue.allTriangleImage.get(2);

        this.bg.getAllEnemy().remove(this);
        this.bg.getRemoveEnemy().add(this);
    }


    // 获取敌人的当前x坐标
    public int getX() {
        return x;
    }

    // 获取敌人的当前y坐标
    public int getY() {
        return y;
    }

    // 获取敌人当前的显示图像
    public BufferedImage getShowImage() {
        return showImage;
    }

    // 设置敌人所属的背景对象
    public void setBg(BackGround bg) {
        this.bg = bg;
    }

    // 获取敌人的类型
    public int getType() {
        return type;
    }

    // 开始敌人的移动
    public void startMove() {
        t.resume();
    }
}
